#
#===============================================================================
#
#         FILE:  Search.pm
#
#  DESCRIPTION:  
#
#        FILES:  ---
#         BUGS:  ---
#        NOTES:  ---
#       AUTHOR:   (), <>
#      COMPANY:  
#      VERSION:  1.0
#      CREATED:  04/14/2010 11:32:02 PM GMT
#     REVISION:  ---
#===============================================================================

use strict;
use warnings;

package GoogPl::Search;

use REST::Google::Search;
use REST::Google::Search::Web;
use REST::Google::Search::Blogs;
use REST::Google::Search::Books;
use REST::Google::Search::Images;
use REST::Google::Search::Local;
use REST::Google::Search::Video;
use REST::Google::Search::News;
use REST::Google::Search::Patent;
use HTML::Entities;
use GoogPl::GooGl;

sub register {
    my $self = shift;

    return ('cmd' => 'search', 'pat' => '^!(search|image|blog|book|local|video|news|patent)\s+(.*)');
}

sub help {
    my $self = shift;

    return (
            'Perform various search functions via Google. Available search commands are: ',
            '!search Google WWW search',
            '!image Google Image search',
            '!blog Google Blog search',
            '!book Google Book search',
            '!local Google Local search, for best results, include city/state or zipcode in search terms',
            '!video Google Video search',
            '!news Google News search',
            '!patent Google Patent search',
           );
}

sub process_command {
    my $self = shift;
    my ($kernel, $sender, $nickhost, $target, $msgtxt, %conf) = @_;

    REST::Google::Search->http_referer($conf{'referer'});

    if( $msgtxt =~ /^!search\s+(.*)/ ) {
        my $res = REST::Google::Search::Web->new('q'=>$1,'safe'=>'off');
        return if $res->responseStatus != 200;
        my $data = $res->responseData;
        return if ! scalar @{$data->results};
        my $r = ${$data->results}[0];
        my $content = $r->content;
        $content =~ s/<.*?>//g;
        $kernel->post($sender => privmsg => $target => 
                sprintf('%s (%s)', decode_entities($content), $r->unescapedUrl));
        return;
    }
    elsif( $msgtxt =~ /^!image\s+(.*)/ ) {
        my $res = REST::Google::Search::Images->new('q'=>$1,'safe'=>'off');
        return if $res->responseStatus != 200;
        my $data = $res->responseData;
        return if ! scalar @{$data->results};
        my $r = ${$data->results}[0];
        my $content = $r->content;
        $content =~ s/<.*?>//g;
        my $responseMsg = decode_entities($content);
        if (length($r->unescapedUrl) > $conf{'min_length'}) {
            my $shortUrl = GoogPl::GooGl::get_short_url($r->unescapedUrl, %conf);
            $responseMsg .= sprintf(' (%s / %s)', $shortUrl, $r->unescapedUrl);
        } else {
            $responseMsg .= sprintf(' (%s)', $r->unescapedUrl);
        }
        $kernel->post($sender => privmsg => $target => $responseMsg);
        return;
    }
    elsif( $msgtxt =~ /^!blog\s+(.*)/ ) {
        my $res = REST::Google::Search::Blogs->new('q'=>$1);
        return if $res->responseStatus != 200;
        my $data = $res->responseData;
        return if ! scalar @{$data->results};
        my $r = ${$data->results}[0];
        my $content = $r->content;
        $content =~ s/<.*?>//g;
        $kernel->post($sender => privmsg => $target => 
                sprintf('%s (%s)', decode_entities($content), $r->postUrl));
        return;
    }
    elsif( $msgtxt =~ /^!book\s+(.*)/ ) {
        my $res = REST::Google::Search::Books->new('q'=>$1);
        return if $res->responseStatus != 200;
        my $data = $res->responseData;
        return if ! scalar @{$data->results};
        my $r = ${$data->results}[0];
        $kernel->post($sender => privmsg => $target => 
                sprintf('%s / %s (%s) %s', $r->authors, $r->titleNoFormatting, $r->bookId, $r->unescapedUrl));
        return;
    }
    elsif( $msgtxt =~ /^!local\s+(.*)/ ) {
        my $res = REST::Google::Search::Local->new('q'=>$1);
        return if $res->responseStatus != 200;
        my $data = $res->responseData;
        return if ! scalar @{$data->results};
        my $r = ${$data->results}[0];
        $kernel->post($sender => privmsg => $target => 
                sprintf('%s, %s, %s, %s [%s]', $r->titleNoFormatting,
                    $r->streetAddress, $r->city, $r->region, $r->staticMapUrl));
        return;
    }
    elsif( $msgtxt =~ /^!video\s+(.*)/ ) {
        my $res = REST::Google::Search::Video->new('q'=>$1);
        return if $res->responseStatus != 200;
        my $data = $res->responseData;
        return if ! scalar @{$data->results};
        my $r = ${$data->results}[0];
        my $content = $r->content;
        $content =~ s/<.*?>//g;
        $kernel->post($sender => privmsg => $target => 
                sprintf('%s (%s)', decode_entities($content), $r->playUrl));
        return;
    }
    elsif( $msgtxt =~ /^!news\s+(.*)/ ) {
        my $res = REST::Google::Search::News->new('q'=>$1);
        return if $res->responseStatus != 200;
        my $data = $res->responseData;
        return if ! scalar @{$data->results};
        my $r = ${$data->results}[0];
        my $content = $r->content;
        $content =~ s/<.*?>//g;
        $kernel->post($sender => privmsg => $target => 
                sprintf('%s (%s)', decode_entities($content), $r->unescapedUrl));
        return;
    }
    elsif( $msgtxt =~ /^!patent\s+(.*)/ ) {
        my $res = REST::Google::Search::Patent->new('q'=>$1);
        return if $res->responseStatus != 200;
        my $data = $res->responseData;
        return if ! scalar @{$data->results};
        my $r = ${$data->results}[0];
        my $content = $r->content;
        $content =~ s/<.*?>//g;
        $kernel->post($sender => privmsg => $target => 
                sprintf('%s - %s / %s (%s)', $r->patentNumber, $r->titleNoFormatting, $content, $r->unescapedUrl));
        return;
    }
}

1;
